package com.mascotikas.client.widgets;

import java.util.List;

import com.google.gwt.core.client.GWT;


import com.google.gwt.user.client.rpc.AsyncCallback;
import com.mascotikas.client.services.IUserImageService;
import com.mascotikas.client.services.IUserImageServiceAsync;
import com.mascotikas.shared.UploadedImage;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.tile.TileGrid;

public class GalleryImage extends TileGrid {

//    private static PhotoGalleryUiBinder uiBinder = GWT.create(PhotoGalleryUiBinder.class);
    IUserImageServiceAsync userImageService = GWT.create(IUserImageService.class);

    public GalleryImage() {
        refreshGallery();
    }

    public void refreshGallery() {
        userImageService.getRecentlyUploaded(new AsyncCallback<List<UploadedImage>>() {

            @Override
            public void onSuccess(List<UploadedImage> images) {
                for (final UploadedImage image : images) {
                    Img imageWidget = createImageWidget(image);
                    addChild(imageWidget);
                }
            }

            @Override
            public void onFailure(Throwable caught) {
                // TODO Auto-generated method stub
            }
        });
    }

    private Img createImageWidget(final UploadedImage image) {
        final Img imageWidget = new Img();
        imageWidget.setSrc(image.getServingUrl() + "=s200");
        return imageWidget;
    }
}
